/************************************************************************
*************************************************************************
*************************************************************************

prodestcon_chile.do

Runs appendix tests in the Chilean manufacturing data used by Gandhi et al.

*************************************************************************
*************************************************************************
************************************************************************/


//Load sharetest
do "do/programs/sharetest.do"

/*****************************************************************
******************************************************************
Helper programs
******************************************************************
*****************************************************************/

/*****************************************************************
Product
*****************************************************************/

//Recursive program to get the pairwise products of a set of terms
cap program drop product
program product
	syntax varlist
	
	gettoken firstvar resvars : varlist	
		
	//Take the product
	foreach var of varlist `varlist' {
		gen `firstvar'X`var' = `firstvar'*`var'
	}
	
	//If we're not on the last word, keep going
	if !mi(word("`resvars'",1)) {
		//Continue recursion
		product `resvars'
	}
end





/*****************************************************************
Formdata
*****************************************************************/
/*
This program simply applies the code given to me by Salvador Navarro
to form the Chilean data.

The only argument is the sector we want to keep
*/


cap program drop formdata
program formdata
		args ind
		
		
		keep if ciiu_3d==`ind'

		gen adv=.
		replace adv=0 if adverts==0 & adverts~=.
		replace adv=1 if adverts>0 & adverts~=.

		gen skwages=wageswc+ bonuswc+ prtaxwc+ fataxwc
		replace skwages=. if skwages<=0
		gen unskwages=wagesbc+ bonusbc+ prtaxbc+ fataxbc
		replace unskwages=. if unskwages<=0
		gen totwages=skwages+unskwages
		replace totwages=. if totwages<=0
		gen ratiowcbc = whitelab / bluelab
		replace ratiowcbc = 10000 if ratiowcbc == . & bluelab == 0 & whitelab ~= 0

		gen output= salegds + shipgds + resales - goodspch + contract + conrepai + coninsta + prodbld + prodmach + prodveh + elecsval + finvufgd + finvfgds - iinvufgd - iinvfgds
		replace output=. if output<=0
		gen matls = totrawma - finvrm + iinvrm + watival + watdval + greasval + lubeval
		replace matls=. if matls<=0
		replace realmats=. if realmats<=0
		gen energ = coalval + carbval +cokeval + petrval + diesval + benzval + parafval + lgasval + pgasval + fwoodval + ofuelval + elecbval
		replace energ=. if energ<=0 
		replace renerg=. if renerg<=0 
		gen serv = cstwtp+freight+insuran+communic+techsupp+accnting+inhouse+salescom+othserv
		replace serv=. if serv<=0
		replace rserv=. if rserv<=0

		gen GO=output/10
		gen RGO=routput/10
		replace RGO=. if RGO<=0

		gen VA=valadded/10
		gen RVA=rva/10
		drop rva
		replace RVA=. if RVA<=0

		gen WI=(energ+matls+serv)/10
		gen RI=(renerg+realmats+rserv)/10
		gen RN=renerg/10
		gen RV=rserv/10
		gen RM=realmats/10
		gen RMV=RM+RV
		gen RNV=RN+RV
		gen WN=energ/10
		gen WV=serv/10
		gen WM=matls/10
		gen WMV=WM+WV
		gen WNV=WN+WV

		gen L = totlab
		gen WL = totwages/10
		gen WLS= skwages/10
		gen WLU = unskwages/10


		gen cpi1985=cpi/16.6436
		gen RL=WL/cpi85
		gen RLS=WLS/cpi85
		gen RLU=WLU/cpi85

		gen K = rcapstock/10
		replace K=. if K<=0

		gen netinv = rinvest/10
		replace netinv=. if netinv<=0
		gen nominv=invnbld + invnmach + invnveh + invubld + invumach + invuveh - salesbld -salesuma - salesuve + impbld + impmach + impveh + prodbld + prodmach + prodveh
		replace nominv=nominv/10
		replace nominv=. if nominv<=0

		gen si = log(WI/GO)
		gen sl = log(WL/GO)
		gen sn = log(WN/GO)
		gen snv = log(WNV/GO)

		replace defoutput=defoutput/100

		//=================================================================================================================================================
		//=================================================================================================================================================
		// Specific to each industry
		//=================================================================================================================================================
		//=================================================================================================================================================
		//replace si=. if si>1.5
		//replace K=. if K>2500
		//=================================================================================================================================================
		//=================================================================================================================================================
		//=================================================================================================================================================
		//=================================================================================================================================================

		gen export=.
		replace export=0 if exports~=. & year>1989
		replace export=1 if exports~=. & exports>0
		gen import=.
		replace import=0 if rawmatsi~=. & rawmatsi==0
		replace import=1 if rawmatsi~=. & rawmatsi>0

		gen wages=.
		replace wages=totwages/L
		bys year: egen mm=median(wages) if wages~=.
		gen hiwag=.
		replace hiwag=0 if wages<mm & wages!=.
		replace hiwag=1 if wages>mm & wages!=.
		drop mm

		keep id year GO RGO VA RVA RN WN RV WV RM WM RMV WMV RNV WNV RI WI RL WL RLS WLS RLU WLU K netinv nominv L si sl sn snv defoutput export import hiwag adv ratiowcbc ///
				faland stassets XX152 XX147 XX148 XX149 XX150 XX151 netprof 

		reshape wide GO RGO VA RVA RN WN RV WV RM WM RMV WMV RNV WNV RI WI RL WL RLS WLS RLU WLU K netinv nominv L si sl sn snv defoutput export import hiwag adv ratiowcbc faland stassets XX152 XX147 XX148 XX149 XX150 XX151 netprof , i(id) j(year)
		reshape long GO RGO VA RVA RN WN RV WV RM WM RMV WMV RNV WNV RI WI RL WL RLS WLS RLU WLU K netinv nominv L si sl sn snv defoutput export import hiwag adv ratiowcbc faland stassets XX152 XX147 XX148 XX149 XX150 XX151 netprof , i(id) j(year)

		bys year: egen def=mean(defoutput)
		drop defoutput

		rename netinv INV

		foreach i of varlist GO RGO VA RVA RN RV RM RMV RNV RI RL RLS RLU K INV nominv L def{
			gen log`i'=log(`i')
		}	

		compress
		sort id year
		save miss_data_jan_28_2011, replace

		bys year: egen tot=sum(RGO)
		gen s=RGO/tot
		gen temp=s*ln(RGO)
		bys year: egen q_index=sum(temp)
		drop temp
		gen temp=s*RGO
		bys year: egen q_index_l=sum(temp)
		drop tot s temp

		foreach i of varlist GO RGO VA RVA RN RV RM RMV RNV RI RL RLS RLU K INV nominv L def{
			replace log`i'=-1000.0 if `i'==. | `i'<=0
		}
		 
		foreach i of varlist GO RGO VA RVA RN WN RV WV RM WM RMV WMV RNV WNV RI WI RL WL RLS WLS RLU WLU K INV nominv L {
			gen I`i'=1
			replace I`i'=0 if `i'==. | `i'<=0
			replace `i'=-1000.0 if I`i'==0
		}	

		foreach i of varlist si sl sn snv {
			gen I`i'=1
			replace I`i'=0 if `i'==.
			replace `i'=-1000.0 if I`i'==0
		}

		replace export=-1000 if export==.
		replace import=-1000 if import==.
		bys year: egen mm=median(logK) if logK>-900
		gen sizek=.
		replace sizek=0 if logK<mm & logK>-999
		replace sizek=1 if logK>=mm
		drop mm
		replace sizek=-1000 if sizek==.

		bys year: egen mm=median(ratiowcbc) 
		gen wcbc=.
		replace wcbc=0 if ratiowcbc<=mm
		replace wcbc=1 if ratiowcbc>mm
		drop mm ratiowcbc
		replace wcbc=-1000 if wcbc==.

		gen l_q_index=ln(q_index)
		gen l_q_index_l=ln(q_index_l)
		drop def logdef q_index q_index_l

		replace hiwag=-1000 if hiwag==.
		replace adv=-1000 if adv==.

		#delimit;
		order GO logGO IGO RGO logRGO IRGO VA logVA IVA RVA logRVA IRVA si Isi sl Isl sn Isn snv Isnv RN logRN IRN WN IWN RV logRV IRV WV IWV 
			RM logRM IRM WM IWM RMV logRMV IRMV WMV IWMV RNV logRNV IRNV WNV IWNV
			RI logRI IRI WI IWI RL logRL IRL WL IWL 
			RLS logRLS IRLS WLS IWLS RLU logRLU IRLU WLU IWLU K logK IK id year 
			INV logINV IINV nominv lognominv Inominv L logL IL l_q_index l_q_index_l 
			export import sizek hiwag adv wcbc; 
		#delimit cr
		sort id year
		compress
		
		//This command is my own; I don't know why log(0) was set to -1000, but I undo it here;
		//since this has to be done to match GNR's OLS results, I'm pretty sure they do the same thing
		recode logL logK logRI logRGO (-1000 = .)

end



/*****************************************************************
******************************************************************
Run Tests
******************************************************************
*****************************************************************/

local industrylist 311 321 322 331 381

//Want to control for year dummies?
local tcon i.t

use "dta/ChileAnalysis2.dta", clear

local count = 0
foreach ind of numlist `industrylist' {
	local count = `count' + 1

	di "On industry `ind'..."
	
	quietly {
		preserve
		/*****************************************************************
		Make industry data
		*****************************************************************/
		
		formdata `ind'
		
		
		//Make variables with friendlier names
		gen t = year
		xtset id t
		
		gen y = logRGO
		gen k = logK
		gen l = logL
		gen m = logRI
		
		//Get higher-order terms
		product k l m
		

		
		sort id t

		/********
		Share test
		********/
		gen logSHARE = si if si != -1000
		
		//Test for different degrees
		forvalues d=2/8 {
			sharetest logSHARE k l m, test( l2.( c.k##c.m ) ) lagtest deg(`d') lagdeg(1) con(i.t)
			
			foreach stat in N N_clust F pval r2 {
				gen shtest_`stat'_d`d' = r(`stat')
			}
		}
		

		gen ind = `ind'
		collapse ind shtest_* 
		
		tempfile `ind'
		save ``ind''
		
		restore
	}
		
	

}

clear
foreach ind in `industrylist' {
	if _N == 0 {
		use ``ind''
	}
	else {
		append using ``ind''
	}
}

order ind

cap mkdir "dta/emp_results"
save "dta/emp_results/chile.dta", replace
